import request from '@/js/request'
import {CONTENT_TYPE_FORM_URLENCODED} from '@/js/api/requestHeaderUtils'
import {serialParameter} from '@/js/api/requestParameterUtils'

const BASE_URL = '/model/form/item/info'

/**
 * 获取表单项基础信息列表
 * @param formId
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getModelFormItems(formId) {
  return request.get(BASE_URL, {params: {formId}})
}

/**
 * 给模型表单添加表单项
 * @param formId 表单id
 * @param attrCodes 模型属性code数组
 * @returns {Promise<AxiosResponse<any>>}
 */
export function addModelFormItems(formId, attrCodes) {
  return request.post(BASE_URL,
    serialParameter({formId, attrCodes: attrCodes.join(',')}),
    {
      headers: {'Content-Type': CONTENT_TYPE_FORM_URLENCODED}
    })
}

/**
 * 删除选中的模型表单项信息
 * @param ids 模型表单项id数组
 * @return {Promise<AxiosResponse<any>>}
 */
export function deleteItems(ids) {
  return request.delete(BASE_URL, {params: {ids: ids.join(',')}})
}

/**
 * 调整表单条目顺序
 * @param formItemId 选中调整的表单项id
 * @param swap 调整标志(值：up,down,top,bottom)
 * @return {Promise<AxiosResponse<any>>}
 */
export function swapFormItem(formItemId, swap) {
  return request.get(BASE_URL + '/swap', {params: {formItemId, swap}})
}
